package com.spy.system.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.spy.common.utils.DateUtils;
import com.spy.system.mapper.SysSupplierDeliveryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spy.system.mapper.SysSupplierMapper;
import com.spy.system.domain.SysSupplier;
import com.spy.system.service.ISysSupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 供应商基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-09
 */
@Service
public class SysSupplierServiceImpl extends ServiceImpl<SysSupplierMapper, SysSupplier> implements ISysSupplierService
{
    @Autowired
    private SysSupplierMapper sysSupplierMapper;
    @Autowired
    private SysSupplierDeliveryMapper deliveryMapper;



    /**
     * 查询供应商基本信息
     * 
     * @param id 供应商基本信息主键
     * @return 供应商基本信息
     */
    @Override
    public SysSupplier selectSysSupplierById(Long id)
    {
                return getById(id);
    }

    /**
     * 查询供应商基本信息列表
     * 
     * @param sysSupplier 供应商基本信息
     * @return 供应商基本信息
     */
    @Override
    public List<SysSupplier> selectSysSupplierList(SysSupplier sysSupplier)
    {
        return sysSupplierMapper.selectSysSupplierList(sysSupplier);
    }

    @Override
    public List<SysSupplier> selectSysSupplierRankList(SysSupplier sysSupplier) {
        // 调用Mapper的排行查询方法（后面会加这个Mapper方法）
        return sysSupplierMapper.selectSysSupplierRankList(sysSupplier);
    }

    /**
     * 新增供应商基本信息
     * 
     * @param sysSupplier 供应商基本信息
     * @return 结果
     */
    @Override
    public int insertSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setCreateTime(DateUtils.getNowDate());
                return save(sysSupplier) ? 1 : 0;
    }

    /**
     * 修改供应商基本信息
     * 
     * @param sysSupplier 供应商基本信息
     * @return 结果
     */
    @Override
    public int updateSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setUpdateTime(DateUtils.getNowDate());
                return updateById(sysSupplier) ? 1 : 0;
    }

    /**
     * 批量删除供应商基本信息
     * 
     * @param ids 需要删除的供应商基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierByIds(Long[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除供应商基本信息信息
     * 
     * @param id 供应商基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }

    @Override
    public Map<String, Integer> getSupplierDeliveryRatio(String timeRange) {
        // 1. 计算时间范围（示例：近1月 = 当前时间 - 30天）
        Date startTime = getStartTimeByTimeRange(timeRange);
        // 2. 按供应商分组统计采购总数
        List<Map<String, Object>> ratioList = deliveryMapper.selectDeliveryRatio(startTime);
        // 3. 转换为Map返回（供应商名称 -> 占比）
        Map<String, Integer> ratioMap = new HashMap<>();
        ratioList.forEach(item -> {
            ratioMap.put((String) item.get("supplierName"), ((Integer) item.get("totalQuantity")));
        });
        return ratioMap;
    }



    // 辅助方法：根据时间范围计算起始时间
    private Date getStartTimeByTimeRange(String timeRange) {
        Calendar calendar = Calendar.getInstance();
        switch (timeRange) {
            case "近1月":
                calendar.add(Calendar.MONTH, -1);
                break;
            case "近2月":
                calendar.add(Calendar.MONTH, -2);
                break;
            // 其他时间范围同理...
        }
        return calendar.getTime();
    }
    @Override
    public List<Map<String, Object>> calculateSupplierScore() {
        List<SysSupplier> supplierList = sysSupplierMapper.selectSysSupplierRankList(new SysSupplier());
        List<Map<String, Object>> scoreList = new ArrayList<>();

        for (SysSupplier supplier : supplierList) {
            Map<String, Object> scoreMap = new HashMap<>();
            scoreMap.put("supplierName", supplier.getSupplierName());
            // 示例：菜品得分、菜价得分、综合得分（可根据实际业务调整逻辑）
            BigDecimal foodScore = new BigDecimal(Math.random() * 2 + 8).setScale(1, BigDecimal.ROUND_HALF_UP);
            BigDecimal priceScore = new BigDecimal(Math.random() * 2 + 7).setScale(1, BigDecimal.ROUND_HALF_UP);
            BigDecimal totalScore = foodScore.add(priceScore).divide(new BigDecimal("2"), 2, BigDecimal.ROUND_HALF_UP);

            scoreMap.put("foodScore", foodScore);
            scoreMap.put("priceScore", priceScore);
            scoreMap.put("totalScore", totalScore);
            scoreList.add(scoreMap);
        }
        return scoreList;
    }
}
